import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;

public class shunxu<T> implements Sequential<T>{
    public int UsedSize;
    public T elem[];
    public shunxu(){
        this.elem=(T[]) new Object[10];
    }
    public void isFull(){
        if(this.UsedSize==this.elem.length){
            expand();
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        shunxu<?> shunxu = (shunxu<?>) o;
        return UsedSize == shunxu.UsedSize && Arrays.equals(elem, shunxu.elem);
    }

    @Override
    public int hashCode() {
        int result = Objects.hash(UsedSize);
        result = 31 * result + Arrays.hashCode(elem);
        return result;
    }

    public void expand(){
        this.elem= Arrays.copyOf(this.elem,2*this.elem.length);
    }

    @Override
    public void add(T data) {
        //判断数组是否满
        isFull();
        this.elem[UsedSize]=data;
        this.UsedSize++;
    }

    @Override
    public void add(int pos, T data) {
        isFull();
        for (int i = this.elem.length-1; i >=pos; i++) {
            this.elem[i+1]=this.elem[i];
        }
        this.elem[pos]=data;
        this.UsedSize++;
    }

    @Override
    public boolean contains(T toFind) {
        for (int i = 0; i < this.elem.length; i++) {
            if(this.elem[i].equals(toFind)){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(T toFind) {
        for (int i = 0; i < this.elem.length; i++) {
            if(this.elem[i].equals(toFind)){
                return i;
            }
        }
        return -1;
    }

    public void checkPos(int pos){
        if(pos<0||pos>this.UsedSize){
            throw new PosExp("pos位置不合法");
        }
    }
    @Override
    public T get(int pos) {
        //检测pos位置是否合法
        try{
            checkPos(pos);
        }catch (PosExp e){
            e.printStackTrace();
        }

        return this.elem[pos];
    }

    @Override
    public void set(int pos, T value) {
        try{
            checkPos(pos);
        }catch (PosExp e){
            e.printStackTrace();
        }
        this.elem[pos]=value;
        this.UsedSize++;
    }

    @Override
    public void remove(T toRemove) {
        int a=indexOf(toRemove);
        if(a==-1){
            System.out.println("没有要删除的数字");
            return;
        }
        for (int i = a; i < this.UsedSize; i++) {
            this.elem[i]=this.elem[i+1];
        }
        this.UsedSize--;
    }

    @Override
    public int size() {

        return this.UsedSize;
    }

    @Override
    public void clear() {
        this.UsedSize=0;
    }

    @Override
    public void display() {
        for (int i = 0; i < this.UsedSize; i++) {
            System.out.println(this.elem[i]);
        }
    }

}
